package com.yanchangapp.ui.activity.purchase.contract;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yanchangapp.R;
import com.yanchangapp.ui.presenter.ClauseEditAtPresenter;
import com.yanchangapp.ui.view.ClauseEditAtView;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.util.ui.UIUtils;
import com.zxl.baselib.widget.NiceNestedScrollView;
import butterknife.BindView;

public class ClauseEditActivity extends BaseActivity<ClauseEditAtView, ClauseEditAtPresenter> implements ClauseEditAtView {

    @BindView(R.id.ivToolbarNavigation)
    ImageView ivToolbarNavigation;
    @BindView(R.id.vToolbarDivision)
    View vToolbarDivision;
    @BindView(R.id.tvToolbarTitle)
    TextView tvToolbarTitle;
    @BindView(R.id.tv_publish_now)
    TextView tvPublishNow;
    @BindView(R.id.tvClauseContent)
    EditText tvClauseContent;
    @BindView(R.id.scrollContent)
    NiceNestedScrollView scrollContent;
    @BindView(R.id.btnNextRefresh)
    TextView btnNextRefresh;
    @BindView(R.id.llErrorNetwork)
    LinearLayout llErrorNetwork;

    private String clause;
    private static final String CLAUSE = "clause";
    public static final int CODE_QUERY_CLAUSE = 0x0090;
    public static void show(Activity activity,String clause){
        Intent intent = new Intent(activity,ClauseEditActivity.class);
        intent.putExtra(CLAUSE,clause);
        activity.startActivityForResult(intent,CODE_QUERY_CLAUSE);
    }

    @Override
    protected void init() {
        // 如果不含数据则返回
        if (getIntent() == null || !getIntent().hasExtra(CLAUSE)){
            finish();
        }else {
            clause = getIntent().getStringExtra(CLAUSE);
        }
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_clause_edit;
    }

    @Override
    protected ClauseEditAtPresenter createPresenter() {
        return new ClauseEditAtPresenter(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvToolbarTitle.setText("合同条款");
        tvPublishNow.setText("确定");
        if (!TextUtils.isEmpty(clause)){
            tvClauseContent.setText(clause);
            tvClauseContent.setSelection(clause.length());
            tvPublishNow.setVisibility(View.VISIBLE);
        }

    }

    @Override
    protected void initData() {
        if (TextUtils.isEmpty(clause)){
            // 如果是空需要进行请求
            mPresenter.getDefaultClause();
        }
    }

    @Override
    protected void initListener() {
        ivToolbarNavigation.setOnClickListener(v -> finish());

        tvPublishNow.setOnClickListener(v -> checkAndBack());

        btnNextRefresh.setOnClickListener(v -> mPresenter.getDefaultClause());
    }

    private void checkAndBack() {
        String innerClause = tvClauseContent.getText().toString();
        if (TextUtils.isEmpty(innerClause)){
            UIUtils.showToast("请输入条款信息");
        }else {
            Intent intent = new Intent();
            intent.putExtra("data",innerClause);
            setResult(RESULT_OK,intent);
            finish();
        }
    }

    @Override
    public void getDefaultClauseSuccess(String data) {
        scrollContent.setVisibility(View.VISIBLE);
        llErrorNetwork.setVisibility(View.GONE);
        // 设置编辑的条款和光标位置...
        data = data.replaceAll("↵ ", "\n");
        tvPublishNow.setVisibility(View.VISIBLE);
        tvClauseContent.setText(data);
        tvClauseContent.setSelection(data.length());
    }

    @Override
    public void getDefaultClauseFailure(String err) {
        scrollContent.setVisibility(View.GONE);
        llErrorNetwork.setVisibility(View.VISIBLE);
    }

}
